package com.ly.lw.styledo.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.ly.lw.styledo.BaseApp;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.adapter.GridLogoAdapter;
import com.ly.lw.styledo.adapter.MyLogoAdapter;
import com.ly.lw.styledo.bean.GridLogo;
import com.ly.lw.styledo.bean.Logo;
import com.ly.lw.styledo.widget.GridViewForScrollView;
import com.ly.lw.styledo.widget.ListViewForScrollView;

import java.util.ArrayList;
import java.util.List;


public class MyGridFragment extends Fragment {
    private GridViewForScrollView gridViewForScrollView;
    private BitmapUtils bitmapUtils;
   // private MyLogoAdapter myLogoAdapter;
   // private List<Logo> listBrandLogo = new ArrayList<>();
    private GridLogoAdapter gridLogoAdapter;
    private List<GridLogo> gridLogoList = new ArrayList<>();
    private DbUtils db;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_like, container, false);
        initView(view);
        loadData();

        return view;
    }



    private void initView(View view) {
        gridViewForScrollView = (GridViewForScrollView) view.findViewById(R.id.my_grid_view);
        //myLogoAdapter = new MyLogoAdapter(gridLogoList,getActivity());
        gridLogoAdapter = new GridLogoAdapter(gridLogoList,getActivity());
        gridViewForScrollView.setAdapter(gridLogoAdapter);
        gridViewForScrollView.setFocusable(false);

        setListViewHeightBasedOnChildren(gridViewForScrollView);

    }


    private void loadData()  {
        if(gridLogoList != null) {
            gridLogoList.clear();
        }

        db = BaseApp.getInstance().getDbUtils();
        try {
            List<GridLogo> gridLogos = db.findAll(GridLogo.class);

            if(gridLogos != null && gridLogos.size()>0) {
                gridLogoList.addAll(gridLogos);
                gridLogoAdapter.notifyDataSetChanged();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }


    }


    private void setListViewHeightBasedOnChildren(GridViewForScrollView gridViewForScrollView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = gridViewForScrollView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, gridViewForScrollView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = gridViewForScrollView.getLayoutParams();
        params.height = totalHeight  ;
        //+ (gridViewForScrollView * (listAdapter.getCount() - 1))
        // listView.getDividerHeight()获取子项间分隔符占用的高度   + (staggeredGridView. * (listAdapter.getCount() - 1))
        // params.height最后得到整个ListView完整显示需要的高度
        gridViewForScrollView.setLayoutParams(params);
    }


    @Override
    public void onStart() {
        super.onStart();
        loadData();
    }



}
